from datetime import datetime

from sqlalchemy.orm import relationship

from apps.model.serializeModel import model_to_dict
from comm.sqlalchemyConfig import db
from apps.settingApi.api.basic.model import basicDataModel

class timerTaskConfigModel(db.Model):
    """
    推送任务
    """
    __tablename__ = 'tx_timer_task_config'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    time = db.Column(db.Integer, nullable=True,default=5,doc='间隔时间')
    status = db.Column(db.Boolean, nullable=True, default=0, doc='是否启用')
    process = db.Column(db.String(125), nullable=True, default="", doc='启动的进程')

    @classmethod
    def getPk(cls):
        timerTaskConfig = db.session.query(cls).first()
        timerTaskConfigData = {}
        timerTaskConfigData['id'] = timerTaskConfig.id
        timerTaskConfigData['time'] = timerTaskConfig.time
        timerTaskConfigData['status'] = timerTaskConfig.status
        timerTaskConfigData['process'] = timerTaskConfig.process
        return timerTaskConfigData


class timerTaskModel(db.Model):
    """
    推送任务
    """
    __tablename__ = 'tx_timer_task'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    name = db.Column(db.String(125), nullable=False,doc='任务名称')
    type = db.Column(db.String(125), nullable=False,doc='任务归属类型，当前是u9capi')
    code = db.Column(db.String(125), nullable=False,doc='任务对应的方法代号')
    code_def = db.Column(db.String(125), nullable=True,default='',doc='任务对应的自定义方法代号')
    sort = db.Column(db.Integer, nullable=True,default=100,doc='排序')
    order_type = db.Column(db.Integer, nullable=True,default=1,doc='单据类型1基础数据，2单据')
    status = db.Column(db.Boolean, nullable=True, default=0, doc='是否启用')
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)
    rem = db.Column(db.Text, nullable=True, default='', doc='备注')

    def __str__(self):
        return 'timer_task:{}'.format(self.name)

    @classmethod
    def getPk(cls,pk):
        timerTaskObj = cls.taskInfo(pk=pk,isTask=False)
        return timerTaskObj


class timerTaskLogModel(db.Model):
    """
    推送任务
    """
    __tablename__ = 'tx_timer_task_log'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    name = db.Column(db.String(200), nullable=True, default='', doc='定时任务名称')
    order = db.Column(db.String(200), nullable=True, default='', doc='推送单号')
    data = db.Column(db.Text, nullable=True, default='', doc='推送数据')
    status = db.Column(db.Boolean, nullable=True, default=0, doc='成功否')
    is_c = db.Column(db.Boolean, nullable=True, default=0, doc='是否处理，0表示未处理，1表示已经处理')
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)
    rem = db.Column(db.Text, nullable=True, default="", doc='说明')

